package com.lsp.server.product.controller;

import com.lsp.server.common.Apiresponse;
import com.lsp.server.product.domain.po.ProductType;
import com.lsp.server.product.service.TypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ProductController
 * @Description 商品分类控制层
 * @Author Mr Hao
 * @Date 2021/11/21 13:34
 */
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService productService;


    /**
     * 获取分类树
     * @return
     */
    @PostMapping("/list")
    public Apiresponse getlist(){
        try {
            return Apiresponse.success(productService.getlist());
        }catch (Exception e){
            return Apiresponse.Error(500,"获取列表失败");
        }
    }

    /**
     * 新增分类
     * @param form
     * @return
     */
    @PostMapping("/add")
    public Apiresponse addType(ProductType form){
        if (null == form.getPid()){
            return Apiresponse.Error(400,"参数错误");
        }
        if (StringUtils.isEmpty(form.getName())){
            return Apiresponse.Error(400,"参数错误");
        }
        productService.addType(form);
        return Apiresponse.success("添加成功");
    }

    @RequestMapping("/queryAttrAndVales")
    public Apiresponse queryAttrAndVales(Integer tid){
        return Apiresponse.success(productService.queryTypeAttrDatas(tid));
    }

    /**将数据同步到redis*/
    @PostMapping("/refresh")
    public Apiresponse refresh(){
        return Apiresponse.success(productService.refresh());
    }
}
